package nextee.cabletest;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.io.*;

/**
 * Created by ycj on 2015/3/24.
 */
public class MainApp extends Application{
    public interface AppCloseEventListener{
        void handleAppCloseEvent();
    }

    public static final int SAMPLE_SIZE = 50;

    private AppCloseEventListener appCloseEventListener;

    public MainApp(){
        extractLibrary();
    }

    //读配置文件
    public static String readConf(String key){
        try {
            Configuration conf = new PropertiesConfiguration("system.conf");
            return new String(conf.getString(key).getBytes("ISO8859-1"), "utf-8");
        }catch (Exception e){
            return "";
        }
    }

    //读配置文件
    public static String readConf(String key, String defaultValue){
        try {
            Configuration conf = new PropertiesConfiguration("system.conf");
            return new String(conf.getString(key).getBytes("ISO8859-1"), "utf-8");
        }catch (Exception e){
            return defaultValue;
        }
    }

    @Override
    public void start(final Stage primaryStage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/main.fxml"));
        Parent root = fxmlLoader.load();
        appCloseEventListener = fxmlLoader.getController();

        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle(readConf("title"));
        primaryStage.setMaximized(true);

		//应用程序关闭前释放占用资源
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                appCloseEventListener.handleAppCloseEvent();
            }
        });
        primaryStage.show();
    }

    //根据平台架构拷贝相应版本的串口代码库
    public void extractLibrary(){
        File file = new File("rxtxSerial.dll");
        if(file.exists()){
            return;
        }

        InputStream inputStream;
        if(System.getProperty("sun.arch.data.model").equals("32")){
            inputStream = getClass().getResourceAsStream("/rxtx/windows/x86/rxtxSerial.dll");
        }
        else{
            inputStream = getClass().getResourceAsStream("/rxtx/windows/x86_64/rxtxSerial64.dll");
        }
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        FileOutputStream fileOutputStream;
        try {
            fileOutputStream = new FileOutputStream("rxtxSerial.dll");
            byte[] buf = new byte[1024];
            int len = bufferedInputStream.read(buf);
            while(len>0){
                fileOutputStream.write(buf, 0, len);
                len = bufferedInputStream.read(buf);
            }
            inputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        launch(args);
    }
}
